/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;

namespace FaceCat {
    /// <summary>
    /// 顶部层
    /// </summary>
    public class HeadDiv : FCView {
        /// <summary>
        /// 创建层
        /// </summary>
        /// <param name="calendar">日历</param>
        public HeadDiv(FCCalendar calendar) {
            m_calendar = calendar;
            setFont(new FCFont("Default", 14, true, false, false));
            setHeight(55);
        }

        public String[] m_weekDays = new String[] { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };

        /// <summary>
        /// 日历
        /// </summary>
        public FCCalendar m_calendar;

        /// <summary>
        /// 获取日历视图
        /// </summary>
        public virtual FCCalendar getCalendar()
        {
            return m_calendar;
        }

        /// <summary>
        /// 设置日历视图
        /// </summary>
        public virtual void setCalendar(FCCalendar value)
        {
            m_calendar = value;
        }

        public DateTitle m_dateTitle;

        /// <summary>
        /// 获取日期标题
        /// </summary>
        public virtual DateTitle getDateTitle()
        {
            return m_dateTitle;
        }

        /// <summary>
        /// 设置日期标题
        /// </summary>
        public virtual void setDateTitle(DateTitle value)
        {
            m_dateTitle = value;
        }

        public ArrowButton m_lastBtn;

        /// <summary>
        /// 获取到上个周期的按钮
        /// </summary>
        public virtual ArrowButton getLastBtn()
        {
            return m_lastBtn;
        }

        /// <summary>
        /// 设置到上个周期的按钮
        /// </summary>
        public virtual void setLastBtn(ArrowButton value)
        {
            m_lastBtn = value;
        }

        public ArrowButton m_nextBtn;

        /// <summary>
        /// 获取到下个周期的按钮
        /// </summary>
        public virtual ArrowButton getNextBtn()
        {
            return m_nextBtn;
        }

        /// <summary>
        /// 设置到下个周期的按钮
        /// </summary>
        public virtual void setNextBtn(ArrowButton value)
        {
            m_nextBtn = value;
        }

        /// <summary>
        /// 获取视图类型
        /// </summary>
        /// <returns>视图类型</returns>
        public override String getViewType() {
            return "HeadDiv";
        }

        /// <summary>
        /// 添加视图方法
        /// </summary>
        public override void onLoad() {
            base.onLoad();
            FCHost host = getNative().getHost();
            if (m_dateTitle == null) {
                m_dateTitle = m_calendar.createDateTitle();
                addView(m_dateTitle);
            }
            if (m_lastBtn == null) {
                m_lastBtn = m_calendar.createArrowButton();
                addView(m_lastBtn);
            }
            if (m_nextBtn == null) {
                m_nextBtn = m_calendar.createArrowButton();
                m_nextBtn.setToLast(false);
                addView(m_nextBtn);
            }
        }

        /// <summary>
        /// 重绘背景方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪区域</param>
        public override void onPaintBackground(FCPaint paint, FCRect clipRect) {
            int width = getWidth(), height = getHeight();
            FCRect rect = new FCRect(0, 0, width, height);
            paint.fillRoundRect(getPaintingBackColor(), rect, getCornerRadius());
        }

        /// <summary>
        /// 重绘边线方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪区域</param>
        public override void onPaintBorder(FCPaint paint, FCRect clipRect) {
            int width = getWidth(), height = getHeight();
            paint.drawLine(getPaintingBorderColor(), 1, 0, 0, height - 1, width, height - 1);
        }

        /// <summary>
        /// 重绘前景方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪区域</param>
        public override void onPaintForeground(FCPaint paint, FCRect clipRect) {
            int width = getWidth(), height = getHeight();
            FCCalendarMode mode = m_calendar.getMode();
            //画星期标题
            if (mode == FCCalendarMode.Day) {
                if (m_calendar.m_showWeekend)
                {
                    m_weekDays = new String[] { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
                }
                else
                {
                    m_weekDays = new String[] { "周一", "周二", "周三", "周四", "周五"};
                }
                float left = 0;
                FCSize weekDaySize = new FCSize();
                FCFont font = getFont();
                long textColor = getPaintingTextColor();
                for (int i = 0; i < m_weekDays.Length; i++) {
                    weekDaySize = paint.textSize(m_weekDays[i], font, -1);
                    float textX = left + (width / m_weekDays.Length) / 2F - weekDaySize.cx / 2F;
                    float textY = height - weekDaySize.cy - 2;
                    FCRect tRect = new FCRect(textX, textY, textX + weekDaySize.cx, textY + weekDaySize.cy);
                    paint.drawText(m_weekDays[i], textColor, font, tRect, -1);
                    left += getWidth() / m_weekDays.Length;
                }
            }
        }

        /// <summary>
        /// 布局更新方法
        /// </summary>
        public override void update() {
            base.update();
            int width = getWidth(), height = getHeight();
            if (m_dateTitle != null) {
                m_dateTitle.setLocation(new FCPoint((width - m_dateTitle.getWidth()) / 2, (height - m_dateTitle.getHeight()) / 2));
            }
            if (m_lastBtn != null) {
                m_lastBtn.setLocation(new FCPoint(2, (height - m_lastBtn.getHeight()) / 2));
            }
            if (m_nextBtn != null) {
                m_nextBtn.setLocation(new FCPoint(width - m_nextBtn.getWidth() - 2, (height - m_nextBtn.getHeight()) / 2));
            }
        }
    }
}
